// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'song-6gcd6ljla32ca6a4',
})
//定义db
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  try {
    //因为每次最多只能查1000条，需要做分页处理,把所有数据请求回来
    const count = await db.collection('customerList').count()
    const limit = 1000;//每次查几条
    const total_times = Math.ceil(count.total / limit)//查几次
    let data = []
    for (let i = 1; i <= total_times; i++) {
      await db.collection('customerList').where(_.or([
        {type:event.type,name:event.name},
        {type:event.type,tel:event.tel}
      ])).skip((i-1)*limit).limit(limit).get().then(res=>{
        data = data.concat(res.data)
      })
      console.log(`查第${i}次`)
    }
    return data
  } catch (error) {
    console.error(error)
  }
}